﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Drawing;
using DataBase.Daihoctuonglai;

namespace Dhtl.Service.Export
{
    public class Service_Export : IExport
    {

        public void ExportEmailResToXlsx(string filePath, IList<EmailDangky> EmailRes)
        {
            var newFile = new FileInfo(filePath);
            // ok, we can run the real code of the sample now
            using (var xlPackage = new ExcelPackage(newFile))
            {
                // uncomment this line if you want the XML written out to the outputDir
                //xlPackage.DebugMode = true; 

                // get handle to the existing worksheet
                var worksheet = xlPackage.Workbook.Worksheets.Add("Danh Email Đăng Ký");
                //Create Headers and format them 
                var properties = new string[]
                {
                    "Email",
                    "Ngày Đăng Ký",
                    
                };
                for (int i = 0; i < properties.Length; i++)
                {
                    worksheet.Cells[1, i + 1].Value = properties[i];
                    worksheet.Cells[1, i + 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
                    worksheet.Cells[1, i + 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
                    worksheet.Cells[1, i + 1].Style.Font.Bold = true;
                }


                int row = 2;
                foreach (var p in EmailRes)
                {

                    int col = 1;

                    worksheet.Cells[row, col].Value = p.Email;
                    col++;

                    worksheet.Cells[row, col].Value = string.Format("{0:dd/MM/yyyy}", p.DateCreate);
                    col++;

                    row++;
                }
                // we had better add some document properties to the spreadsheet 

                // set some core property values
                xlPackage.Workbook.Properties.Title = "Danh Sách Đăng ký Email";
                xlPackage.Workbook.Properties.Author = "Lê Tấn Mạnh - Programmer";
                xlPackage.Workbook.Properties.Subject = "Danh Sách Đăng ký Email";
                xlPackage.Workbook.Properties.Keywords = "Danh Sách Đăng ký Email";
                xlPackage.Workbook.Properties.Category = "Phú Thịnh Travel";
                xlPackage.Workbook.Properties.Comments = "Danh Sách Đăng ký Email";

                // set some extended property values
                xlPackage.Workbook.Properties.Company = "Đại Học Tương Lai";
                //xlPackage.Workbook.Properties.HyperlinkBase = new Uri("www.maymayquanglong.com");

                // save the new spreadsheet
                xlPackage.Save();
            }
        }

        public void ExportContactToXlsx(string filePath, IList<Contact> TuVans)
        {
            var newFile = new FileInfo(filePath);
            // ok, we can run the real code of the sample now
            using (var xlPackage = new ExcelPackage(newFile))
            {
                // uncomment this line if you want the XML written out to the outputDir
                //xlPackage.DebugMode = true; 

                // get handle to the existing worksheet
                var worksheet = xlPackage.Workbook.Worksheets.Add("Danh Sách Liên Hệ");
                //Create Headers and format them 
                var properties = new string[]
                {
                    "Tên",
                    "Email",
                    "Điện thoại",
                    "nội dung",
                    "Ngày liên hệ",
                    
                };
                for (int i = 0; i < properties.Length; i++)
                {
                    worksheet.Cells[1, i + 1].Value = properties[i];
                    worksheet.Cells[1, i + 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
                    worksheet.Cells[1, i + 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
                    worksheet.Cells[1, i + 1].Style.Font.Bold = true;
                }


                int row = 2;
                foreach (var p in TuVans)
                {

                    int col = 1;

                    worksheet.Cells[row, col].Value = p.Name;
                    col++;

                    worksheet.Cells[row, col].Value = p.Email;
                    col++;

                    worksheet.Cells[row, col].Value = p.Dt;
                    col++;

                    worksheet.Cells[row, col].Value = p.NoiDung;
                    col++;

                    worksheet.Cells[row, col].Value = string.Format("{0:dd/MM/yyyy}", p.DateCreate);
                    col++;

                    row++;
                }
                // we had better add some document properties to the spreadsheet 

                // set some core property values
                xlPackage.Workbook.Properties.Title = "Danh Sách Liên Hệ";
                xlPackage.Workbook.Properties.Author = "Lê Tấn Mạnh - Programmer";
                xlPackage.Workbook.Properties.Subject = "Danh Sách Liên Hệ";
                xlPackage.Workbook.Properties.Keywords = "Danh Sách Liên Hệ";
                xlPackage.Workbook.Properties.Category = "Phú Thịnh Travel";
                xlPackage.Workbook.Properties.Comments = "Danh Sách Liên Hệ";

                // set some extended property values
                xlPackage.Workbook.Properties.Company = "Đại Học Tương Lai";
                //xlPackage.Workbook.Properties.HyperlinkBase = new Uri("www.maymayquanglong.com");

                // save the new spreadsheet
                xlPackage.Save();
            }
        }

    }
}
